import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/model/weather_model.dart';
import 'package:syn_4s/core/utils/date_format_utils.dart';

import '../../core/constants/sp_key.dart';
import '../../core/utils/sp_utils.dart';
import '../../core/viewmodel/main_view_model.dart';
import '../shared/app_theme.dart';
import 'custom_ui.dart';

class MainInfo extends StatelessWidget {
  final MainViewModel viewModel;

  const MainInfo({super.key, required this.viewModel});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 6.px),
      padding:
          EdgeInsets.only(left: 8.px, right: 8.px, top: 6.px, bottom: 12.px),
      decoration: BoxDecoration(
        color: AppTheme.backgroundColors,
        borderRadius: BorderRadius.all(Radius.circular(4.px)),
      ),
      child: Column(
        children: [
          buildProject(context),
          Container(
            margin: EdgeInsets.only(bottom: 6.px),
            color: Colors.white,
            height: 1.px,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              StreamBuilder<String>(
                  stream: Stream.periodic(const Duration(seconds: 1), (_) {
                    return DateFormatUtils.dateTimeToStr(DateTime.now(),
                        format: "HH:mm:ss | yyyy-MM-dd ");
                  }),
                  builder: (ctx, snapshot) {
                    return Expanded(child: getBlackText(snapshot.data ?? ''));
                  }),
              getSvgPicture("assets/images/location.svg", 22.px),
              Obx((){
                viewModel.getLocation(context);
                return getBlackText("${viewModel.location}");
              })
            ],
          ),
          SizedBox(
            height: 14.px,
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Obx(() {
                Weather? weather = getWeather("TMP");
                return Offstage(
                  offstage: weather == null,
                  child: Row(
                    children: [
                      getSvgPicture("assets/images/temperature.svg", 22.px),
                      getBlackText("${weather?.val ?? "0"}%"),
                    ],
                  ),
                );
              }),
              Obx(() {
                Weather? weather = getWeather("FS");
                return Offstage(
                  offstage: weather == null,
                  child: Row(
                    children: [
                      SizedBox(
                        width: 12.px,
                      ),
                      getSvgPicture("assets/images/air_velocity.svg", 22.px),
                      SizedBox(
                        width: 5.px,
                      ),
                      getBlackText("${weather?.val ?? "0"}km/h")
                    ],
                  ),
                );
              }),
              Obx(() {
                Weather? weather = getWeather("HR");
                return Offstage(
                  offstage: weather == null,
                  child: Row(
                    children: [
                      SizedBox(
                        width: 12.px,
                      ),
                      getSvgPicture("assets/images/humidity_level.svg", 22.px),
                      getBlackText("${weather?.val ?? "0"}%"),
                    ],
                  ),
                );
              }),
            ],
          )
        ],
      ),
    );
  }

  Weather? getWeather(String code) {
    return viewModel.weathers.firstWhere(
      (weather) => weather?.code == code,
      orElse: () => null, // 如果找不到则返回null
    );
  }

  Widget buildProject(BuildContext context) {
    GlobalKey globalKey = GlobalKey();
    return InkWell(
      onTap: () {
        viewModel.myFocusNode.unfocus();
        viewModel.getProjects(context, globalKey);
      },
      child: Obx(() => Row(
            key: globalKey,
            children: [
              Expanded(
                child: Text(
                  textAlign: TextAlign.end,
                  viewModel.projName.value,
                  style: TextStyle(color: Colors.black, fontSize: 15.px),
                ),
              ),
              Icon(
                Icons.arrow_drop_down,
                size: 36.px,
              )
            ],
          )),
    );
  }

  Widget getBlackText(String data) {
    return Text(data, style: const TextStyle(color: AppTheme.infoTextColors));
  }
}
